package cn.hpu.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

public class DateConvertor extends StrutsTypeConverter {
	
	private SimpleDateFormat[] sdfs = {
			new SimpleDateFormat("yyyy-MM-dd")
	};

	@Override
	public Object convertFromString(Map context, String[] values, Class toType) {
		
		String dateStr = values[0];
		
		for(SimpleDateFormat sdf : sdfs) {
			
			try {
				return sdf.parse(dateStr);
			} catch (ParseException e) {
				continue;
			}
		}
		return null;
	}

	@Override
	public String convertToString(Map context, Object object) {
		
		Date date = (Date)object;
		return new SimpleDateFormat("yyyy-MM-dd").format(date);
	}

}
